import java.util.*;

public class code3_2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int ret = Integer.MAX_VALUE;

        String s1 = in.next();
        String s2 = in.next();

        int cur1 = -1;
        int cur2 = -1;

        for(int i=0;i<n;i++) {
            String s = in.next();
            if(s.equals(s1)) {
                if (cur2 != -1) {
                    ret = Math.min(ret,i-cur2);
                }
                cur1 = i;
            }
            if(s.equals(s2)) {
                if (cur1 != -1) {
                    ret = Math.min(ret,i-cur1);
                }
                cur2 = i;
            }
        }

        System.out.println(ret==Integer.MAX_VALUE?-1:ret);
    }
}
